package com.leetcode.p100;

import com.bean.TreeNode;
import com.utils.TreeUtils;

/**
 *
 */
public class Test108 {

    public TreeNode sortedArrayToBST(int[] nums) {
        return help(nums, 0, nums.length - 1);
    }

    private TreeNode help(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }

        int middle = (right - left) / 2 + left;

        TreeNode treeNode = new TreeNode(nums[middle]);
        treeNode.left = help(nums, left, middle - 1);
        treeNode.right = help(nums, middle + 1, right);
        return treeNode;
    }

    public static void main(String[] args) {
//        TreeNode root = TreeUtils.buildTree();
//        TreeUtils.showTree(root);

        Test108 test = new Test108();
        TreeNode result = test.sortedArrayToBST(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
//        TreeNode result = test.sortedArrayToBST(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
//        TreeNode result = test.sortedArrayToBST(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14});
//        TreeNode result = test.sortedArrayToBST(new int[]{1, 2, 3, 4, 5, 6});
//        TreeNode result = test.sortedArrayToBST(new int[]{1, 2, 3, 4});
//        TreeNode result = test.sortedArrayToBST(new int[]{1, 2});
        TreeUtils.showTree(result);
    }

}
